class Login{
     returnURL = '';
    constructor(){

         //设置成功登录后的回跳地址
        //获取?后的参数
        let param = location.search;
        // console.log(param);
        if(param){
            param = param.split('=')[1];
            this.returnUrl = param;
        }
        

       //给登陆按钮绑定事件
    //    console.log(this.$('.submit'));
    this.$('.submit1').addEventListener('click',this.checkLogin)
    //给注册按钮绑定点击事件
    this.$('.submit').addEventListener('click',this.newUser);

    //登陆和注册绑定点击事件
    this.$('.zhuce').addEventListener('click',this.zhuce.bind(this))
    this.$('.denglu').addEventListener('click',this.login.bind(this))
    }
    
    checkLogin=()=>{
    //    console.log(111111);
    //获取框中的value
    let username = this.$('#name').value.trim();
    let password = this.$('#psd').value.trim();
    // console.log(username,password);

     //进行非空验证
     if(!username){
        layer.tips('用户名不能为空','#name')
        return
    }
    if(!password){
        layer.tips('密码不能为空','#psd')
        return
    }

    //发送ajax请求登陆
     // 设置编码方式
     axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
     let param = `username=${username}&password=${password}`
    //  console.log(param);
    axios.post('http://localhost:8888/users/login',param).then(({status,data})=>{
        //判断登陆是否成功
    if(status!=200||data.code!=1){
        layer.msg('登陆失败')
        return;
    }

     //储存token
     localStorage.setItem('token',data.token);
     console.log(data);
    //储存用户id
    localStorage.setItem('user_id',data.user.id);

    //回跳到原来的页面,returnUrl如果有值,则返回原来的页面,如果为空,则返回search.html页面
    this.returnUrl ? location.assign(this.returnUrl) : location.assign('./search.html');
    })
    
    }
   

    //登陆注册
    login(){
    //   console.log(111);
//    console.log(this);
      this.$('.show').style.display = 'none';
      this.$('.hide').style.display = 'block'
    }
    zhuce(){
        console.log(222);
       this.$('.hide').style.display = 'none'
       this.$('.show').style.display = 'block';
    }
    //注册
    newUser=()=>{
        //获取input框里的value
        let username = this.$('#uname').value.trim();
        let password = this.$('#password').value.trim();
        let sure = this.$('.sure').value.trim();
        let nicheng = this.$('.nicheng').value.trim();
         //进行非空验证
     if(!username){
        layer.tips('用户名不能为空','#uname')
        return
    }
    if(!password){
        layer.tips('密码不能为空','#password')
        return
    }
    if(!sure){
        layer.tips('密码不能为空','.sure')
        return
    }
    if(!nicheng){
        layer.tips('昵称不能为空','.nicheng')
        return
    }

    //正则判断
    let reg = /^[a-z0-9]\w{4,11}$/;
    let reg1 = /\w{6,12}/;
    if(!reg.test(username)){
        layer.tips('用户名格式错误','#uname')
        return
    }
    if(!reg1.test(password)){
        layer.tips('密码格式错误','#password')
        return
    }
    if(password!=sure){
        layer.tips('两次密码输入的不一致','.sure')
        return
    }

    axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    //发送ajax请求
    let param = `username=${username}&password=${password}&rpassword=${sure}&nickname=${nicheng}`
    axios.post('http://localhost:8888/users/register',param).then(({ status,data })=>{
        // console.log(res);
         if(status==200&&data.code==1){
            this.$('.show').style.display = 'none';
            this.$('.hide').style.display = 'block'
         }
    })
     
}

    //封装获取节点的方法
$(ele){
    let res = document.querySelectorAll(ele);
    return res.length==1 ? res[0] : res;
}
}

new Login;